<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	自定义小程序
	diy小程序页面功能：设计小程序
	业务流程：选择模版——创建小程序
	注意事项：微官网小程序和diy小程序为同一个页面的不同表现形式
	选择模版的话，需要一个小程序模版库，模版库参考微站模版模版库
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>'0');
	if($_RQ['group_id']){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 24;
	$total='';
	$fields=array('id','title','group_id','image');
	$list=pdo_getslice('core_aliapp_template',$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	if($list){
		$group_templates=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'aliapp_template');
		foreach($list as &$item){
			//根据模版价格，结合用户套餐、用户购买情况，判断是否需要购买
			$time=pdo_getcolumn('core_account_permissions',array('uniacid'=>$_SESSION['uniacid'],'type'=>5,'pid'=>$item['id']),'time');
			if($item['price'] && !in_array($item['id'],$group_templates) && (!$time || $time<TIMESTAMP)){
				$item['buy']=1;
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	/*$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'公共模版库'),
			array('op'=>'my','title'=>'我的模版'),
			array('op'=>'add','title'=>'直接添加'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'模版名称','type'=>'string'),
			array('name'=>'type','title'=>'模版类型','type'=>'select','options'=>'types'),
			array('op'=>'add','title'=>'使用模版','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);*/
	$return['groups']=pdo_getall('core_template_group',array(),array('id','title'),'id');
	$all=array('id'=>0,'title'=>'全部');
	array_unshift($return['groups'],$all);
	exi($return);
}
if($_RQ['op']=='my'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['group_id']){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 24;
	$total='';
	$fields=array('id','title','group_id','image');
	$list=pdo_getslice('core_aliapp_template',$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['types']=pdo_getall('core_template_group',array(),array('id','title'),'id');
	/*$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'公共模版库'),
			array('op'=>'my','title'=>'我的模版'),
			array('op'=>'add','title'=>'直接添加'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'模版名称','type'=>'string'),
			array('name'=>'type','title'=>'模版类型','type'=>'select','options'=>'types'),
			array('op'=>'add','title'=>'使用模版','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);*/
	$return['groups']=pdo_getall('core_template_group',array(),array('id','title'),'id');
	$all=array('id'=>0,'title'=>'全部');
	array_unshift($return['groups'],$all);
	exi($return);
}
if($_RQ['op']=='add'){
	//添加小程序
	$id=intval($_RQ['id']);
	if($_SESSION['account']['groupid']){
			$limit=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'aliapp_num');
			$num=pdo_count('core_account_aliapp',array('uniacid'=>$_SESSION['uniacid']));
			if($num>=$limit){
				exi('您所添加的小程序已达到最大值，如需创建更多，请联系管理员提升权限或者升级套餐！','error');
			}
		}
	$insert=array(
		'title'=>'未设置',
		'uniacid'=>$_SESSION['uniacid'],
		'type'=>1,
		'createtime'=>TIMESTAMP
	);
	if($id){
		//判断是否免费、是否购买
		
		//读取数据
		$data=pdo_get('core_aliapp_template',array('id'=>$id),array('items','setting'));
		$insert['items']=$data['items'];
		$insert['setting']=$data['setting'];
	}
	pdo_insert('core_account_aliapp',$insert);
	$web_id=pdo_insertid();
	$action=array('action'=>'aliapp.list','op'=>'design','query'=>'id='.$web_id);
	exi('','success',$action);
}